package com.ruoyi.project.cms.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cms.domain.CmsPrice;
import com.ruoyi.project.cms.service.ICmsPriceService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 系统价格配置Controller
 * 
 * @author ruoyi
 * @date 2020-08-31
 */
@RestController
@RequestMapping("/cms/price")
public class CmsPriceController extends BaseController
{
    @Autowired
    private ICmsPriceService cmsPriceService;

    /**
     * 查询系统价格配置列表
     */
    @PreAuthorize("@ss.hasPermi('cms:price:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsPrice cmsPrice)
    {
        startPage();
        List<CmsPrice> list = cmsPriceService.selectCmsPriceList(cmsPrice);
        return getDataTable(list);
    }

    /**
     * 导出系统价格配置列表
     */
    @PreAuthorize("@ss.hasPermi('cms:price:export')")
    @Log(title = "系统价格配置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CmsPrice cmsPrice)
    {
        List<CmsPrice> list = cmsPriceService.selectCmsPriceList(cmsPrice);
        ExcelUtil<CmsPrice> util = new ExcelUtil<CmsPrice>(CmsPrice.class);
        return util.exportExcel(list, "price");
    }

    /**
     * 获取系统价格配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:price:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmsPriceService.selectCmsPriceById(id));
    }

    /**
     * 新增系统价格配置
     */
    @PreAuthorize("@ss.hasPermi('cms:price:add')")
    @Log(title = "系统价格配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsPrice cmsPrice)
    {
        CmsPrice cmsPrice1 = cmsPriceService.selectCmsPriceByPriceTypeAndCarType(cmsPrice.getPriceType(), cmsPrice.getCarType());
        if (null != cmsPrice1){
            return AjaxResult.error("您已配置过同类型的价格信息，请勿重复配置");
        }
        return toAjax(cmsPriceService.insertCmsPrice(cmsPrice));
    }

    /**
     * 修改系统价格配置
     */
    @PreAuthorize("@ss.hasPermi('cms:price:edit')")
    @Log(title = "系统价格配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsPrice cmsPrice)
    {
        return toAjax(cmsPriceService.updateCmsPrice(cmsPrice));
    }

    /**
     * 删除系统价格配置
     */
    @PreAuthorize("@ss.hasPermi('cms:price:remove')")
    @Log(title = "系统价格配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmsPriceService.deleteCmsPriceByIds(ids));
    }
}
